package bb.lanxing.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import bb.lanxing.R;
import bb.lanxing.util.DensityUtil;
import bb.lanxing.util.UserAvatarUtil;

public class UserAvatarView extends RelativeLayout {
    public static final int AVATAR_SIZE_60 = 1;
    public static final int AVATAR_SIZE_48 = 2;
    public static final int AVATAR_SIZE_36 = 3;
    public static final int AVATAR_SIZE_24 = 4;
    private static final String TAG = "UserAvatarView";
    private ImageView avatar;
    private TextView avatarLevel;
    private Context context;
    private ImageView mIvFrame;
    private RelativeLayout mRlAvatar;
    private TextView mTvProTitle;

    public UserAvatarView(Context context) {
        this(context, null);
    }

    public UserAvatarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UserAvatarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public UserAvatarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    public void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.user_avatar, this, true);
        avatar = findViewById(R.id.ivAvatar);
        mIvFrame = findViewById(R.id.iv_frame);
        avatarLevel = findViewById(R.id.tvLevel);
        mTvProTitle = findViewById(R.id.tv_pro_title);
        mRlAvatar = findViewById(R.id.rl_avatar);
    }

    public void setAvatarMode(int mode) {
        if (mode == AVATAR_SIZE_60) {
            setAvatar(72, 68, 8, 70, 11, 6);
        } else if (mode == AVATAR_SIZE_48) {
            setAvatar(60, 56, 8, 58, 9, 5);
        } else if (mode == AVATAR_SIZE_36) {
            setAvatar(45, 42, 7, 44, 6, 3);
        } else if (mode == AVATAR_SIZE_24) {
            setAvatar(28, 26, 7, 28, 0, 0);
        }
    }

    private void setAvatar(int rlSize, int avatarSize, int levelSize, int frameSize, int proTitleSize, int proTitlePadding) {
        ViewGroup.LayoutParams layoutParams = mRlAvatar.getLayoutParams();
        layoutParams.height = DensityUtil.dp2px(rlSize);
        float f = frameSize;
        layoutParams.width = DensityUtil.dp2px(f);
        mRlAvatar.setLayoutParams(layoutParams);
        ViewGroup.LayoutParams layoutParams2 = avatar.getLayoutParams();
        if (layoutParams2 == null) {
            layoutParams2 = new ViewGroup.LayoutParams(-2, -2);
        }
        float f2 = avatarSize;
        layoutParams2.height = DensityUtil.dp2px(f2);
        layoutParams2.width = DensityUtil.dp2px(f2);
        avatar.setLayoutParams(layoutParams2);
        ViewGroup.LayoutParams layoutParams3 = mIvFrame.getLayoutParams();
        if (layoutParams3 == null) {
            layoutParams3 = new ViewGroup.LayoutParams(-2, -2);
        }
        layoutParams3.height = DensityUtil.dp2px(f);
        layoutParams3.width = DensityUtil.dp2px(f);
        mIvFrame.setLayoutParams(layoutParams3);
        avatarLevel.setTextSize(levelSize);
        if (proTitleSize == 0) {
            mTvProTitle.setVisibility(View.GONE);
            avatarLevel.setVisibility(View.GONE);
            return;
        }
        mTvProTitle.setTextSize(proTitleSize);
        float f4 = (float) proTitlePadding / 2;
        mTvProTitle.setPadding(DensityUtil.dp2px((float) proTitlePadding), DensityUtil.dp2px(f4), DensityUtil.dp2px((float) proTitlePadding), DensityUtil.dp2px(f4));
    }

    public void enableAvatarBorder() {
        avatar.setBackgroundResource(R.drawable.ring_32dip_white_1side);
    }

    public void setNormalAvatar(String photoURL) {
        avatarLevel.setVisibility(View.GONE);
        UserAvatarUtil.getInstance().setUserAvatarUrl(avatar, photoURL);
    }

    public void setAvatarForUrl(String url) {
        UserAvatarUtil.getInstance().setUserAvatarUrl(avatar, url);
    }

    public void setProTitle(String title) {
        if (title == null) {
            mTvProTitle.setVisibility(View.GONE);
            return;
        }
        mTvProTitle.setVisibility(View.VISIBLE);
        mTvProTitle.setText(title);
    }

    public void isShowAvatarFrame(boolean isShow) {
        mIvFrame.setVisibility(isShow ? View.VISIBLE : View.GONE);
        avatarLevel.setVisibility(isShow ? View.GONE : View.VISIBLE);
    }

    public void setAvatarUrl(String avatarUrl) {
        UserAvatarUtil.getInstance().setUserAvatarUrl(avatar, avatarUrl);
    }

    public void resetAvatar() {
        avatar.setImageResource(R.drawable.ic_v1_profile);
        setUserLevelText(0);
        setProTitle(null);
        mIvFrame.setVisibility(View.GONE);
    }

    public void setUserLevelText(int level) {
        if (level <= 0) {
            avatarLevel.setVisibility(View.GONE);
            return;
        }
        avatarLevel.setVisibility(View.VISIBLE);
        int paddingBottom = avatarLevel.getPaddingBottom();
        int paddingTop = avatarLevel.getPaddingTop();
        int paddingRight = avatarLevel.getPaddingRight();
        int paddingLeft = avatarLevel.getPaddingLeft();
        switch (level) {
            case 1, 2, 3 -> avatarLevel.setBackgroundResource(R.drawable.level_1_3);
            case 4, 5, 6 -> avatarLevel.setBackgroundResource(R.drawable.level_4_6);
            case 7, 8, 9 -> avatarLevel.setBackgroundResource(R.drawable.level_7_9);
            case 10, 11, 12 -> avatarLevel.setBackgroundResource(R.drawable.level_10_12);
            case 13, 14, 15 -> avatarLevel.setBackgroundResource(R.drawable.level_13_15);
            default -> avatarLevel.setBackgroundResource(R.drawable.level_13_15);
        }
        avatarLevel.setText("Lv." + level);
        avatarLevel.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
    }

    public void goToUserInfo(final long userId) {
        setOnClickListener(v -> UserAvatarUtil.getInstance().goToUserInfo(context, userId));
    }

    public ImageView getAvatar() {
        return avatar;
    }

}